/**
 *
 * Copyright 2008 davethedev@googlemail.com
 * 
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


package nethelp.network;

import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
import javax.swing.JLabel;
import javax.swing.JTable;

public class MXLookup {

    private String mailHost;
    private JLabel countLabel;
    private JTable mXTable;

    public MXLookup(String mailHost, JLabel countLabel, JTable mXTable) throws NamingException {
        this.mailHost = mailHost;
        this.countLabel = countLabel;
        this.mXTable = mXTable;
        
        lookupMx();
    }

    int lookupMx() throws NamingException {
        Hashtable env = new Hashtable();
        env.put("java.naming.factory.initial",
                "com.sun.jndi.dns.DnsContextFactory");
        DirContext ictx = new InitialDirContext(env);
        Attributes attrs =
                ictx.getAttributes(mailHost, new String[]{"MX"});
        Attribute attr = attrs.get("MX");
        if (attr == null) {
            return (0);
        }
        
        countLabel.setText(attr.size() + " Mail records found");
        MXRecordTableModel model = new MXRecordTableModel(attr);
        mXTable.setModel(model);

        return (attr.size());
    }
}